<template>
  <el-dialog
    :title="'用户注册'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="65%"
    fullscreen>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="邮箱" prop="email" :class="{ 'is-required': true }">
      <el-input v-model="dataForm.email" placeholder="作为登录账号"></el-input>
    </el-form-item>
    <el-form-item label="用户账号" prop="realname" :class="{'is-required': true}">
      <el-input v-model="dataForm.realname" placeholder="只能由汉字组成"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password" :class="{ 'is-required': true }">
      <el-input v-model="dataForm.password" type="password" placeholder="字母、数字或者英文符号，最短8位，区分大小写"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="comfirmPassword" :class="{ 'is-required': true }">
      <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="请再次输入密码"></el-input>
    </el-form-item>
    <el-form-item label="级别" prop="level" :class="{ 'is-required': true }">
      <el-select v-model="dataForm.level" clearable placeholder="请选择" @change="loadDistrict" >
        <el-option label="省级用户" value="1"></el-option>
        <el-option label="市级用户" value="2"></el-option>
        <el-option label="县级用户" value="3"></el-option>
        <el-option label="填报人" value="4"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="省" prop="province" :class="{ 'is-required': true }" v-if="visibleProvince">
          <el-select v-model="dataForm.province" clearable placeholder="请选择" @change="loadCity">
            <el-option
              v-for="item in provinceList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
     <el-form-item label="市" prop="city" :class="{ 'is-required': true }" v-if="visibleCity">
          <el-select v-model="dataForm.city" clearable  placeholder="请选择" @change="loadCountry">
            <el-option
              v-for="item in cityList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>

      <el-form-item label="县" prop="country" :class="{ 'is-required': true }" v-if="visibleCountry">
          <el-select v-model="dataForm.country" clearable placeholder="请选择">
            <el-option
              v-for="item in countryList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
     <el-form-item label="单位" prop="employer" :class="{ 'is-required': true }">
          <el-input v-model="dataForm.employer" placeholder="只能由汉字、字母组成"></el-input>
        </el-form-item>
      <el-form-item label="职务" prop="position" :class="{ 'is-required': true }">
          <el-input v-model="dataForm.position" placeholder="只能由汉字、字母组成"></el-input>
        </el-form-item>
      <el-form-item label="手机号" prop="mobile" :class="{ 'is-required': true }">
          <el-input v-model="dataForm.mobile" placeholder="请输入11位的有效手机号"></el-input>
        </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer"> 
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">注册</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validateComfirmPassword = (rule, value, callback) => {
        if (!/\S/.test(value)) {
          callback(new Error('确认密码不能为空'))
        } else if (this.dataForm.password !== value) {
          callback(new Error('确认密码与密码输入不一致'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        visibleProvince: false,
        visibleCity: false,
        visibleCountry: false,
         provinceList:[],
        cityList:[],
        countryList:[],
        dataForm: {
          email: '',
          realname: '',
          password: '',
          confirmPassword: '',
          level: '',
          employer:'',
          position:'',
          mobile:'',
        },
        dataRule: {
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          realname: [
            { required: true, message: '用户账号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          comfirmPassword: [
            { validator: validateComfirmPassword, trigger: 'blur' }
          ],
          level: [
            { required: true, message: '级别不能为空', trigger: 'blur' }
          ],
          employer: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          position: [
            { required: true, message: '职务不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
        }
      }
    },

    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/register`),
              method: 'post',
              data: this.$http.adornData({
                'email': this.dataForm.email,
                'username': this.dataForm.realname,
                'password': this.dataForm.password,
                'level': this.dataForm.level,
                'employer':this.dataForm.employer,
                'position':this.dataForm.position,
                'mobile':this.dataForm.mobile,
                'province':this.dataForm.province,
                'city':this.dataForm.city,
                'country':this.dataForm.country
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 加载区域
      loadDistrict(curVal){
         this.loadProvince();
        if(curVal == '1'){
          this.visibleProvince = true;
           this.visibleCity = false;
            this.visibleCountry = false;
        }else if(curVal == '2'){
          this.visibleProvince = true;
          this.visibleCity = true;
          this.visibleCountry = false;
        }else{
          this.visibleProvince = true;
          this.visibleCity = true;
          this.visibleCountry = true;
        }
      },
      //加载省级行政区
      loadProvince(){
        this.provinceList = [];
        this.$http({
            url: this.$http.adornUrl('/sys/register/select'),
            method: 'get',
            params: this.$http.adornParams({
              'level': 1
            })
          }).then(({data}) => {
            this.provinceList = data.list;
          })
      },
      //加载市级行政区
      loadCity(curVal){
        this.cityList = [];
        this.$http({
            url: this.$http.adornUrl('/sys/register/select'),
            method: 'get',
            params: this.$http.adornParams({
              'level': 2,
              'parentCode': curVal
            })
          }).then(({data}) => {
            this.cityList = data.list;
          })
      },
      //加载县级行政区
      loadCountry(curVal){
        this.countryList = [];
        this.$http({
            url: this.$http.adornUrl('/sys/register/select'),
            method: 'get',
            params: this.$http.adornParams({
              'level': 3,
              'parentCode': curVal
            })
          }).then(({data}) => {
            this.countryList = data.list;
          })
      }
    }
  }
</script>
